package com.ezcoding.dao;

import com.ezcoding.model.Dingdan;
import com.ezcoding.model.DingdanDetail;
import com.jfinal.plugin.activerecord.Db;

import java.util.List;

public class DingdanDao {
    public static DingdanDao dao = new DingdanDao();

    public List<Dingdan> getDingdansByUsername(
            String sourceUsername,
            int type,
            int state) {

        String sql = Db.getSql("Dingdan.getDingdansByUsernameAndTypeAndState");

        return Dingdan.dao.find(sql, sourceUsername, type, state);
    }

    public List<Dingdan> getImportDingdans(
            String username,
            int state){
        String sql = Db.getSql("Dingdan.getImportDingdans");

        return Dingdan.dao.find(sql,username,state);
    }

    public Dingdan getDingdan(String username, String dingdanId) {
        String sql = Db.getSql("Dingdan.getDingdanByUsernameAndDingdanId");

        return Dingdan.dao.findFirst(sql, username, dingdanId);
    }

    public int deleteDingdanDetails(String username, String dingdanId) {
        String sql = Db.getSql("Dingdan.deleteDingdanDetailsByUsernameAndDingdanId");

        return Db.update(sql, username, dingdanId);
    }

    public List<DingdanDetail> getDingdanDetails(String username, String dingdanId) {
        String sql = Db.getSql("Dingdan.getDingdanDetailsByUsernameAndDingdanId");

        return DingdanDetail.dao.find(sql, username, dingdanId);
    }

    public List<Dingdan> getDingdans(
            String sourceUsername,
            String targetUsername,
            String startTime,
            String endTime,
            int type) {
        if (type == 0) {
            //Sale
            return getSaleDingdans(
                    sourceUsername,
                    targetUsername,
                    startTime,
                    endTime);
        } else {
            //Purchase
            return getPurchaseDingdans(
                    sourceUsername,
                    targetUsername,
                    startTime,
                    endTime);
        }
    }

    public List<DingdanDetail> getDingdanDetails(
            String sourceUsername,
            String targetUsername,
            String productId,
            String startTime,
            String endTime,
            int type) {
        if (type == 0) {
            return getSaleDingdanDetails(sourceUsername, targetUsername, productId, startTime, endTime);
        } else {
            return getPurchaseDingdanDetails(sourceUsername, targetUsername, productId, startTime, endTime);
        }
    }

    private List<Dingdan> getSaleDingdans(
            String sourceUsername,
            String targetUsername,
            String startTime,
            String endTime) {
        String sql = Db.getSql("Dingdan.getSaleDingdansBySourceUsername");

        return Dingdan.dao.find(sql, sourceUsername, "%" + targetUsername, startTime, endTime);
    }

    private List<Dingdan> getPurchaseDingdans(
            String sourceUsername,
            String targetUsername,
            String startTime,
            String endTime) {
        String sql = Db.getSql("Dingdan.getPurchaseDingdansBySourceUsername");

        return Dingdan.dao.find(sql, sourceUsername, "%" + targetUsername, startTime, endTime);
    }

    private List<DingdanDetail> getSaleDingdanDetails(
            String sourceUsername,
            String targetUsername,
            String productId,
            String startTime,
            String endTime) {
        String sql = Db.getSql("Dingdan.getSaleDingdanDetailsBySourceUsername");

        return DingdanDetail.dao.find(sql, sourceUsername, "%" + targetUsername, "%" + productId, startTime, endTime);
    }

    private List<DingdanDetail> getPurchaseDingdanDetails(
            String sourceUsername,
            String targetUsername,
            String productId,
            String startTime,
            String endTime) {
        String sql = Db.getSql("Dingdan.getPurchaseDingdanDetailsBySourceUsername");

        return DingdanDetail.dao.find(sql, sourceUsername, "%" + targetUsername, "%" + productId, startTime, endTime);
    }

    public List<DingdanDetail> getDingdanDetails(
            String sourceUsername,
            String targetUsername,
            int type) {
        switch (type) {
            case Dingdan.DINGDAN_FIN_TYPE_TICKET_IN:
                return getTicketInDingdanDetails(sourceUsername, targetUsername);
            case Dingdan.DINGDAN_FIN_TYPE_TICKET_OUT:
                return getTicketOutDingdanDetails(sourceUsername, targetUsername);
            case Dingdan.DINGDAN_FIN_TYPE_MONEY_IN:
                return getMoneyInDingdanDetails(sourceUsername, targetUsername);
            case Dingdan.DINGDAN_FIN_TYPE_MONEY_OUT:
                return getMoneyOutDingdanDetails(sourceUsername, targetUsername);
        }

        return null;
    }

    private List<DingdanDetail> getTicketInDingdanDetails(String sourceUsername, String targetUsername) {
        String sql = Db.getSql("Dingdan.getTicketInDingdanDetail");

        return DingdanDetail.dao.find(sql, sourceUsername, targetUsername);
    }

    private List<DingdanDetail> getTicketOutDingdanDetails(String sourceUsername, String targetUsername) {
        String sql = Db.getSql("Dingdan.getTicketOutDingdanDetail");

        return DingdanDetail.dao.find(sql, sourceUsername, targetUsername);
    }

    private List<DingdanDetail> getMoneyInDingdanDetails(String sourceUsername, String targetUsername) {
        String sql = Db.getSql("Dingdan.getMoneyInDingdanDetail");

        return DingdanDetail.dao.find(sql, sourceUsername, targetUsername);
    }

    private List<DingdanDetail> getMoneyOutDingdanDetails(String sourceUsername, String targetUsername) {
        String sql = Db.getSql("Dingdan.getMoneyOutDingdanDetail");

        return DingdanDetail.dao.find(sql, sourceUsername, targetUsername);
    }
}
